package isstra.gestore;
import sde.monitor.*;
public class GestoreMH implements Gestore{ //Soluzione col monitor di Hoare
	private boolean release=false;
	private int cnt=0, w=0;
	private Monitor lock=new Monitor(true);
	private Condition attesa=lock.newCondition();
	public void request(){
		lock.Enter();
		try{
			if( release ){
				w++;
				attesa.Wait();
				w--;
			}
			cnt++;
			System.out.println(Thread.currentThread().getName()+" entra");
			if( cnt==3 ) release=true;
			if( !release && w>0 ) attesa.Signal();
		}finally{
			lock.Exit();
		}
	}//request
	public void finish(){
		lock.Enter();
		try{
			cnt--;
			System.out.println(Thread.currentThread().getName()+" esce");
			if( cnt==0 ){
				release=false;
				if( w>0) attesa.Signal();
			}
		}finally{
			lock.Exit();
		}
	}//finish
}//GestoreMH
